package com.kt.coroutinedemo.utils;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by qinjie on 2018/8/14.
 */

public class DateTime {

    public final static String DEFAULT = "00-00-00 00:00:00";

    private DateFormat DEFAULT_FORMAT = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.getDefault());
    private DateFormat DF_HH_MM_SS_12_FORMAT = new SimpleDateFormat("yy-MM-dd hh:mm:ss a", Locale.getDefault());

    private int year; /*[0, 99]*/
    private int month; /*[1, 12]*/
    private int day; /*[1, 31]*/
    private int hour; /*[0, 23]*/
    private int minute; /*[0, 59]*/
    private int second; /*[0, 59]*/
    private int week; /*[0, 6]，0:sunday; 1:monday*/
    private int reserve; /*[0, 59]*/

    public DateTime(int year, int month, int day, int hour, int minute, int second, int week) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        this.week = week;
    }

    public DateTime(int year, int month, int day, int hour, int minute, int second) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        this.week = 0;
    }

    public DateTime(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = 0;
        this.minute = 0;
        this.second = 0;
        this.week = 0;
    }


    public DateTime(Long time) {
        Date date = new Date(time);
        DEFAULT_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
        String mDate = DEFAULT_FORMAT.format(date);

        try{
            this.year = Integer.parseInt(mDate.substring(0, 2));
            this.month = Integer.parseInt(mDate.substring(3, 5));
            this.day = Integer.parseInt(mDate.substring(6, 8));
            this.hour = Integer.parseInt(mDate.substring(9, 11));
            this.minute = Integer.parseInt(mDate.substring(12, 14));
            this.second = Integer.parseInt(mDate.substring(15, 17));
        }catch (NumberFormatException e){
            this.year = 0;
            this.month = 0;
            this.day = 0;
            this.hour = 0;
            this.minute = 0;
            this.second = 0;
        }
    }

//    /**
//     * 通过时间来创建
//     *
//     * @param time yyyy-MM-dd HH:mm:ss
//     * @date 20181217
//     * @author shixin
//     */
//    public DateTime(String time) {
//        this.year = Integer.parseInt(time.substring(0, 4));
//        this.month = Integer.parseInt(time.substring(5, 7));
//        this.day = Integer.parseInt(time.substring(8, 10));
//        this.hour = Integer.parseInt(time.substring(11, 13));
//        this.minute = Integer.parseInt(time.substring(14, 16));
//        this.second = Integer.parseInt(time.substring(17, 19));
//    }

    /**
     * 通过时间来创建
     *
     * @param time yy-MM-dd HH:mm:ss
     * @date 20181217
     * @author shixin
     */
    public DateTime(String time) {
        try{
            this.year = Integer.parseInt(time.substring(0, 2));
            this.month = Integer.parseInt(time.substring(3, 5));
            this.day = Integer.parseInt(time.substring(6, 8));
            this.hour = Integer.parseInt(time.substring(9, 11));
            this.minute = Integer.parseInt(time.substring(12, 14));
            this.second = Integer.parseInt(time.substring(15, 17));
        }catch (Exception e){
            this.year = 0;
            this.month = 0;
            this.day = 0;
            this.hour = 0;
            this.minute = 0;
            this.second = 0;
        }
    }

    /**
     * 根据DateTime_t解析
     *
     * @param data
     */
    public DateTime(byte[] data) {
        year = DecodeUtils.INSTANCE.bytesToInteger16(data, 0, 1);
        month = DecodeUtils.INSTANCE.bytesToInteger16(data, 1, 1);
        day = DecodeUtils.INSTANCE.bytesToInteger16(data, 2, 1);
        hour = DecodeUtils.INSTANCE.bytesToInteger16(data, 3, 1);
        minute = DecodeUtils.INSTANCE.bytesToInteger16(data, 4, 1);
        second = DecodeUtils.INSTANCE.bytesToInteger16(data, 5, 1);
//        week = DecodeUtils.INSTANCE.bytesToInteger16(data, 6, 1);
//        reserve = DecodeUtils.INSTANCE.bytesToInteger16(data, 7, 1);
        //Log.e("Datetime", "init " + this.toString());
    }


    public Long date2Long() throws ParseException {
//        Calendar cr = Calendar.getInstance();
//        cr.set(year, month, day, hour, minute, second);
        DEFAULT_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
       Date date =  DEFAULT_FORMAT.parse(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
        Long time = date.getTime();
        return time;
    }



    @Override
    public String toString() {
        return "DateTime{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                ", hour=" + hour +
                ", minute=" + minute +
                ", second=" + second +
                ", week=" + week +
                ", reserve=" + reserve +
                '}';
    }

    /**
     * 日期yyyy-MM-DD HH:mm:ss
     *
     * @return
     */
    public String getDate() {
//        String yearStr = year < 2000 ? ((year + 2000) + "") : String.valueOf(year);
        String yearStr = convert(year);
        String monthStr = convert(month);
        String dayStr = convert(day);
        String hourStr = convert(hour);
        String minuteStr = convert(minute);
        String secondStr = convert(second);
        return new StringBuilder()
                .append(yearStr).append("-")
                .append(monthStr).append("-")
                .append(dayStr).append(" ")
                .append(hourStr).append(":")
                .append(minuteStr).append(":")
                .append(secondStr).toString();
    }

    public String getHHmm(){
        return convert(hour) + ":" + convert(minute);
    }

    /**
     * 日期yyyyMMDDHHmmssnn
     *
     * @return yyyyMMDDHHmmssnn
     */
    public String getDate2() {
        String yearStr = year < 2000 ? ((year + 2000) + "") : String.valueOf(year);
        String monthStr = convert(month);
        String dayStr = convert(day);
        String hourStr = convert(hour);
        String minuteStr = convert(minute);
        String secondStr = convert(second);
        String weekStr = convert(week);
        String reserveStr = convert(reserve);
        Log.e("Datetime", "getDate2 " + this.toString());
        return new StringBuilder()
                .append(yearStr)
                .append(monthStr)
                .append(dayStr)
                .append(hourStr)
                .append(minuteStr)
                .append(secondStr)
                .append(weekStr)
                .append(reserveStr).toString();
    }

    private String convert(int value) {
        return value < 10 ? ("0" + value) : ("" + value);
    }


    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getWeek() {
        return week;
    }

    public void setWeek(int week) {
        this.week = week;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    public int getReserve() {
        return reserve;
    }

    public void setReserve(int reserve) {
        this.reserve = reserve;
    }


    /**
     * 获取实际显示时间
     *
     * @param is24Format 24小时格式
     * @return 24小时格式时间或者12小时格式时间
     */
    public String getRealShowTime(boolean is24Format) {
        String dataTime = getDate();
        if (is24Format) {
            return dataTime;
        } else {
            try {
                Date parse = DEFAULT_FORMAT.parse(dataTime);
                String format = DF_HH_MM_SS_12_FORMAT.format(parse);
                return format;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return DateTime.DEFAULT;
    }
}
